package com.xingkeduo.service.api.marketing.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xingkeduo.dao.TActivityDaysMapper;
import com.xingkeduo.dao.TMarketingCardMapper;
import com.xingkeduo.dao.TMarketingCouponBagMapper;
import com.xingkeduo.dao.TMarketingCouponMapper;
import com.xingkeduo.feign.HairstyleFeignService;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.model.po.TMarketing;
import com.xingkeduo.service.api.marketing.StoreService;
import com.xingkeduo.util.tool.ResponseMessage;

@Service
public class StoreServiceImpl implements StoreService {
	Logger logger = LoggerFactory.getLogger(StoreServiceImpl.class);

	@Autowired
	TMarketingCouponMapper tMarketingCouponMapper;
	@Autowired
	TMarketingCouponBagMapper tMarketingCouponBagMapper;
	@Autowired
	TMarketingCardMapper tMarketingCardMapper;
	@Autowired
	TActivityDaysMapper tActivityDaysMapper;
	@Autowired
	HairstyleFeignService hairstyleFeignService;

	@Override
	public ResponseMessage queryStoresByMarketingId(Long marketingId,Integer marketingType) {
		TMarketing marketing = selectByPrimaryKey(marketingId,marketingType);
		if (marketing == null || StringUtils.isBlank(marketing.getStoreIds())) {
			return ResponseMessage.error("暂无门店信息", 500);
		}
		String storeIds = marketing.getStoreIds();
		if (storeIds.startsWith(",")) {
			storeIds = storeIds.substring(1, storeIds.length());
		}
		if (storeIds.endsWith(",")) {
			storeIds = storeIds.substring(0, storeIds.length()-1);
		}
		if (storeIds.equalsIgnoreCase("0")) {
			Map param = new HashMap();
			param.put("status","2");
			param.put("typeList",Lists.newArrayList(1,2,3));
			String result = hairstyleFeignService.doStoreGet(JSON.toJSONString(param));
			JSONObject resultJson = JSON.parseObject(result);
			String status = resultJson.getOrDefault("status", "0") == null ? "0"
					: resultJson.getOrDefault("status", "0").toString();
			return status.equals("1") ? ResponseMessage.ok(resultJson.get("data"))
					: ResponseMessage.error((resultJson.getOrDefault("msg", "") == null ? ""
							: resultJson.getOrDefault("msg", "").toString()), 500);
		}
		String shopList = hairstyleFeignService.shoplists(storeIds,2,"1,2,3");
		JSONObject shopListJson = JSON.parseObject(shopList);
		String status = shopListJson.getOrDefault("status", "0") == null ? "0"
				: shopListJson.getOrDefault("status", "0").toString();
		if (status.equals("1")) {
			return ResponseMessage.ok(shopListJson.get("data"));
		} else {
			return ResponseMessage.error(
					(shopListJson.getOrDefault("msg", "") == null ? "" : shopListJson.getOrDefault("msg", "").toString()),
					500);
		}
	}
	private TMarketing selectByPrimaryKey(Long supportBusinessId, Integer marketingType) {
		if(marketingType.equals(MarketingTypeEnum.COUPON.getCode())) {
			return tMarketingCouponMapper.selectByPrimaryKey(supportBusinessId);
		}else if(marketingType.equals(MarketingTypeEnum.CARD.getCode())) {
			return tMarketingCardMapper.selectByPrimaryKey(supportBusinessId);
		}else if(marketingType.equals(MarketingTypeEnum.COUPON_BAG.getCode())) {
			return tMarketingCouponBagMapper.selectByPrimaryKey(supportBusinessId);
		}
		return null;
	}

}
